﻿using ExpressesClass.BestApi.Utils;
using ExpressesClass.IExpresses;
using ExpressesClass.Model;
using ExpressesClass.Util;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace OFFICE_Helper.Util
{
    public class ExpressUtil
    {
        public static bool GetMailNo(Kddh kddh)
        {
            IExpress express = GetIExpress.IGet(kddh.kdCompanys);
            if (express != null)
            {
                kddh.orderId = SignUtil.MakeSign(kddh.sendMan).Substring(0, 4).ToUpper() + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                kddh.scenarios = Kddh.ScenariosType.OFFICE.ToString();
                kddh.printNum = int.Parse(kddh.itemCount);

                Kddh k = null;
                try
                {
                    k = express.GetMailNo(kddh);
                }
                catch
                {
                    MessageBox.Show("连接快递接口出错,请重试.", "提示");
                }

                if (k != null && k.mailNo != null)
                {
                    if (DbUtil.Kddh_Insert(k))
                    {
                        MessageBox.Show("获取快递电子面单成功.", "提示");
                        return true;
                    }
                    else
                    {
                        MessageBox.Show("获取快递电子面单成功,但插入数据库失败,请重试.", "提示");
                    }
                }
            }
            return false;
        }
        public static bool GetBatchMailNo(Kddh kddh)
        {
            IExpress express = GetIExpress.IGet(kddh.kdCompanys);
            if (express != null)
            {
                kddh.orderId = SignUtil.MakeSign(kddh.sendMan).Substring(0, 4).ToUpper() + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                kddh.scenarios = Kddh.ScenariosType.OFFICE.ToString();
                kddh.printNum = int.Parse(kddh.itemCount);

                Kddh k = null;
                try
                {
                    k = express.GetMailNo(kddh);
                }
                catch
                {
                }

                if (k != null && k.mailNo != null)
                {
                    if (DbUtil.Kddh_Insert(k))
                    {
                        return true;
                    }
                    else
                    {
                    }
                }
            }
            return false;
        }
        public static bool CancelMailNo(Kddh kddh)
        {
            IExpress express = GetIExpress.IGet(kddh.kdCompanys);
            if (express != null)
            {
                bool b = false;
                try
                {
                    b = express.CancelMailNo(kddh);
                }
                catch
                {
                    MessageBox.Show("连接快递接口出错,请重试.", "提示");
                }

                if (b)
                {
                    if (DbUtil.Kddh_Cancel(kddh.mailNo))
                    {
                        MessageBox.Show("面单号:" + kddh.mailNo + " ,取消快递电子面单成功.", "提示");
                        return true;
                    }
                    else
                    {
                        MessageBox.Show("面单号:" + kddh.mailNo + " ,取消快递电子面单成功,但插入数据库失败,请重试.", "提示");
                    }
                }
            }
            return false;
        }
        public static void GetUseNum(string kdCompanys)
        {
            IExpress express = GetIExpress.IGet(kdCompanys);
            if (express != null)
            {
                try
                {
                    express.GetUseNum();
                }
                catch
                {
                    MessageBox.Show("连接快递接口出错,请重试.", "提示");
                }
            }
        }
        public static List<TraceLog> GetExpressMsg(Kddh kddh)
        {
            IExpress express = GetIExpress.IGet(kddh.kdCompanys);
            if (express != null)
            {
                try
                {
                    return express.GetExpressMsg(kddh);
                }
                catch
                {
                    MessageBox.Show("连接快递接口出错,请重试.", "提示");
                }
            }
            return new List<TraceLog>();
        }
    }
}
